package com.lishe.lishe.lishe.module.me.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.module.me.adapter.PageAdapter;
import com.lishe.lishe.lishe.module.me.fragments.FragmentCash;
import com.lishe.lishe.lishe.module.me.fragments.FragmentCoupons;
import com.lishe.lishe.lishe.module.me.fragments.FragmentPoints;

import java.util.LinkedList;

/**
 * Created by zhcasecom on 2017/8/16.
 * 我的资产
 */

public class MyAccountActivity extends BaseActivity {
    private ViewPager viewPager;
    private TextView texTitle;
    private TabLayout tabLayout;
    private String[] titles;
    private PageAdapter pageAdapter;
    private LinkedList<Fragment> fragmentList = new LinkedList<>();
    private FragmentPoints points_fragment;
    private FragmentCoupons coupons_fragment;
    private FragmentCash cash_fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titles = getResources().getStringArray(R.array.titles);
        setContentView(R.layout.activity_my_account);
        initView();
    }

    private void initView() {
        viewPager = findViewById(R.id.viewpager_assets);
        tabLayout = findViewById(R.id.tabLayout_assets);
        texTitle = findViewById(R.id.title_text_title);
        texTitle.setText("我的资产");
        Button back = findViewById(R.id.title_left_button);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setListener();
        initFragment();
    }


    private void initFragment() {
        fragmentList.clear();
        if (points_fragment == null) {
            points_fragment = FragmentPoints.newInstance();
            fragmentList.add(points_fragment);
        }
        if (coupons_fragment == null) {
            coupons_fragment = FragmentCoupons.newInstance();
            fragmentList.add(coupons_fragment);
        }
        if (cash_fragment == null) {
            cash_fragment = FragmentCash.newInstance();
            fragmentList.add(cash_fragment);
        }
        pageAdapter = new PageAdapter(fragmentList, titles, getSupportFragmentManager());
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(0);

    }

    private void setListener() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            viewPager.setOnScrollChangeListener(new View.OnScrollChangeListener() {

                @Override
                public void onScrollChange(View view, int i, int i1, int i2, int i3) {

                }
            });
        }
    }

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, MyAccountActivity.class);
        activity.startActivity(intent);
    }
}
